package emailserving.domain;

import org.apache.commons.lang.Validate;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

@Entity
@Table(name = "es_contacts")
public class Contact extends AbstractModel {

    private static final Find<Long, Contact> find = new Find<Long, Contact>() {
    };

    public static Contact createByEmail(String email) {
        Validate.notEmpty(email);
        Contact contact = new Contact();
        contact.email = email;
        return contact;
    }


    public Contact(Job job, String email) {
        this.job = job;
        this.email = email;
    }

    private Contact() {
    }

    @ManyToOne
    @JoinColumn(name = "job_id")
    private Job job;

    @Column(name = "email", length = 100, nullable = false)
    private String email;

    @Column(name = "sent_time")
    @Temporal(TemporalType.TIMESTAMP)
    private Date sentTime;

    public static int hadBeenSentEmailCount(Long jobId) {
        return find.where().eq("job_id", jobId).isNotNull("sent_time").findRowCount();
    }

    public static int notBeenSentEmailCount(Long jobId) {
        return find.where().eq("job_id", jobId).isNull("sent_time").findRowCount();
    }


    public static List<Contact> findNotSentContact(long jobId) {
        return find.where().eq("job_id", jobId).isNull("sent_time").findList();
    }

    public static int contactsTotalCount(Long jobId) {
        return find.where().eq("job_id", jobId).findRowCount();
    }

    public static Contact findById(long contactId) {
        return find.where().eq("id", contactId).findUnique();
    }

    public static void updateContactSentTime(long contactId) {
        Contact contact = Contact.findById(contactId);
        if (contact != null) {
            contact.setSentTime(new Date());
            db().update(contact);
        }
    }

    public static boolean existsNotBeenSentContactsOfJob(long jobId) {
        return notBeenSentEmailCount(jobId) > 0;
    }


    public Date getSentTime() {
        return sentTime;
    }

    public void setSentTime(Date sentTime) {
        this.sentTime = sentTime;
    }

    public Job getJob() {
        return job;
    }

    public void setJob(Job job) {
        this.job = job;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Contact contact = (Contact) o;

        if (job != null ? !job.equals(contact.job) : contact.job != null) return false;
        if (email != null ? !email.equals(contact.email) : contact.email != null) return false;
        return !(sentTime != null ? !sentTime.equals(contact.sentTime) : contact.sentTime != null);

    }

    @Override
    public int hashCode() {
        int result = job != null ? job.hashCode() : 0;
        result = 31 * result + (email != null ? email.hashCode() : 0);
        result = 31 * result + (sentTime != null ? sentTime.hashCode() : 0);
        return result;
    }


}
